from config import HOST, PORT, DEBUG
from db.model_loader import model_loader

model_loader.init_model()

from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from api import categories_router, documents_router, search_router, chat_router, auth_router
from api.auth import verify_token


# 创建FastAPI应用
app = FastAPI(title="大模型智能知识库", description="基于ChromaDB的智能文本分类与检索系统")

# 添加CORS中间件以允许跨域请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源，生产环境应限制
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(auth_router)
app.include_router(categories_router, dependencies=[Depends(verify_token)])
app.include_router(documents_router, dependencies=[Depends(verify_token)])
app.include_router(search_router, dependencies=[Depends(verify_token)])
app.include_router(chat_router, dependencies=[Depends(verify_token)])


# API端点
@app.get("/", tags=["根路径"])
async def root():
    return {"message": "欢迎使用大模型智能知识库！"}


# 启动服务器（仅在直接运行时）
if __name__ == "__main__":
    uvicorn.run("main:app", host=HOST, port=PORT, reload=DEBUG, workers=1)  # 生产环境可以增加workers数量
